package com.joinway.admin.controller;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.jsondoc.core.annotation.Api;
import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.annotation.ApiQueryParam;
import org.jsondoc.core.annotation.ApiResponseObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.joinway.admin.bean.view.CodeHistoryView;
import com.joinway.admin.service.AppService;
import com.joinway.framework.admin.core.service.file.FileManager;
import com.joinway.framework.admin.core.service.file.FileManager.Server;
import com.joinway.framework.bean.logging.annotation.InputLog;
import com.joinway.framework.bean.logging.annotation.LogIgnore;
import com.joinway.framework.bean.logging.annotation.OutputLog;
import com.joinway.framework.bean.view.View;
import com.joinway.framework.core.controller.ExceptionController;
import com.joinway.framework.support.audit.annotation.Audit;

@Api(name = "test api", description = "")
@Controller
@RequestMapping("code")
@Validated
public class AutoCodeController extends ExceptionController {

	private final static Logger log = LoggerFactory.getLogger(AutoCodeController.class);
	
	@Autowired AppService service;
	
	@RequestMapping(value="snapshot", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@Audit
	@InputLog
	@OutputLog
	public View saveSnapshot(
			@RequestParam(value="dom",required=false,defaultValue="") @LogIgnore String dom
			, @RequestParam(value="loginUser") String loginUser
			, @RequestParam(value="type") String type
			, @RequestParam(value="table") String table
	) throws Exception {
//		service.saveSnapshot(dom, loginUser, DataUtils.convertToInt(type), table);
		return new View();
	}

	@RequestMapping(value="snapshot", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@Audit
	@InputLog
//	@OutputLog
	public CodeHistoryView getSnapshot(@RequestParam(value="id") String id) throws Exception {
//		return service.findSnapShot(DataUtils.convertToInt(id));
		CodeHistoryView view = new CodeHistoryView();
		view.setHtml("");
		return view;
	}
	
	@RequestMapping(value="apply", produces=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@Audit
	@InputLog
	@OutputLog
	public View applyPage(@RequestParam("localFile") String localFile) throws Exception {
		String dir = FileManager.getLocalPath("tabs", Server.App);
		FileUtils.copyFileToDirectory(new File(localFile), new File(dir), false);
		return new View();
	}
	
	
	@ApiMethod(description="test", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/test.json", produces=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public View test(@ApiQueryParam(name="message") @RequestParam(value="message",required=false,defaultValue="test") String message){
		View view = new View();
		view.setMessage(message);
		return view;
	}
}
